<?php

namespace app\common\controller;

use app\qsapi\model\BoxMemberInfo;
use app\qsapi\model\BoxNotice;
use app\qsapi\model\Member;
use think\cache\driver\Redis;

/**
 * 盒子站内信公共方法
 */
class Notice
{

    /**
     * 消息类型
     * @var string[]
     */
    protected $type = [
        1 => '系统消息',
        2 => '帖子评论提醒',
        3 => '限制',
        4 => '签到提醒',
        5 => '头像审核结果通知',
        6 => '头像审核结果通知',
    ];

    protected $key = '';
    protected $member_id = 0;


    /**
     * 初始化用户
     * @param $member_id
     * @return void
     */
    public function __construct($member_id)
    {
        $this->member_id = $member_id;
        $this->key = 'box_notice_' . $member_id;
    }

    /**
     * 同步用户redis
     * @param $member_id
     * @return true
     */
    public function init()
    {
        $redis = new Redis(getRedisConfig());

        if ($redis->exists( $this->key))   return true;
        //获取所有未读消息
        $list = BoxNotice::where('member_id','=', $this->member_id)->where('read_time', '=', 0)->select();
        if (!$list) return true;
        foreach ($list as $v) {
            $redis->sAdd($this->key, (string)$v['id']);
        }
        return true;
    }

    /**
     * 发送站内信
     * @param $type 消息类型
     * @param $content 消息内容
     * @param $extend_id 扩展id 用户绑定对应消息的内容  例如评论绑定帖子ID
     * @return void
     */
    public function sendToUser($type = 1, $content = '', $extend_id = 0)
    {
        if (!in_array($type, array_keys($this->type))) {
            return false;
        }
        //获取用户信息
        $member_info = BoxMemberInfo::where('member_id', $this->member_id)->find();
        //用户关闭站内信  不发送
        if (!$member_info || $member_info['is_push_notice'] == 0) return true;

        $res = BoxNotice::insertGetId([
            'member_id' => $this->member_id,
            'title' => $this->type[$type],
            'content' => $content,
            'source' => $type,
            'create_time' => time(),
            'extend_id' => $extend_id
        ]);
        //写入用户对应缓存 未读数量
        if ($res) {
            $redis = new Redis(getRedisConfig());
            $redis->sAdd($this->key, $res);
        }
        return true;
    }


    /**
     * 发送站内信(系统)
     * @return void
     */
    public function sendToSystem()
    {

    }

    /**
     * 获取未读消息数量
     */
    public  function  unreadNum(){
        $this->init();
        $redis = new Redis(getRedisConfig());
        $num=$redis->sCard($this->key)??0;

        //获取系统消息未读数量
        $member=(new Member())->where('id',$this->member_id)->find();
        //获取已读的系统消息
        $ids = BoxNotice::where('member_id', $this->member_id)->where('system_id', '>', 0)->column('system_id');
        $system_num = BoxNotice::where('member_id = -1')->where('create_time','>=',$member['create_time'])
            ->whereNotIn('id', $ids)
            ->where('read_time', '=', 0)->count();

        return $num + $system_num;

    }

    /**
     * 已读消息
     * @param array $info
     * @return bool
     */
    public function read(array $info): bool
    {
        if (empty($info)) return true;
        $this->init();
        if ($info['member_id'] != $this->member_id) return true;
        $redis = new Redis(getRedisConfig());
        $redis->sRem($this->key,(string)$info['id']);
        return true;
    }
}